<?php

namespace Unl\HomologacionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Unl\HomologacionBundle\Entity\MallaCurricular;
use Unl\HomologacionBundle\Entity\Universidad;
use Unl\HomologacionBundle\Form\MallaCurricularType;
use Unl\HomologacionBundle\Form\UniversidadType;


/**
 * MallaCurricular controller.
 * @Route("/mallacurricular")
 */
class MallaCurricularController extends BaseController
{
    /**
     * Lists all MallaCurricular entities.
     *
     * @Route("/", name="mallacurricular")
     * @Template()
     */
    public function indexAction()
    {
         
        $sample = new Universidad();
        $sample->setNombre('');
        
        $searchForm = $this->createForm(new UniversidadType(), $sample);
        $request = $this->getRequest();
        $searchForm->bindRequest($request);

        $em = $this->getDoctrine()->getEntityManager();
        $entities = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->findByCriterio($sample->getNombre());
        
        return array('entities' => $entities, 
                     'search_form' => $searchForm->createView());
        
        
        
    }

    /**
     * Finds and displays a MallaCurricular entity.
     *
     * @Route("/{id}/show", name="mallacurricular_show")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find MallaCurricular entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        );
    }

    /**
     * Displays a form to create a new MallaCurricular entity.
     *
     * @Route("/new", name="mallacurricular_new")
     * @Template()
     */
    public function newAction()
    {
        $entity = new MallaCurricular();
        $form   = $this->createForm(new MallaCurricularType(), $entity);

        return array(
            'entity' => $entity,
            'edit_form'   => $form->createView()
        );
    }

    /**
     * Creates a new MallaCurricular entity.
     *
     * @Route("/create", name="mallacurricular_create")
     * @Method("post")
     * @Template("UnlHomologacionBundle:MallaCurricular:new.html.twig")
     */
    public function createAction()
    {
        $em = $this->getDoctrine()->getEntityManager();
        $entity  = new MallaCurricular();
        $request = $this->getRequest();
        $form    = $this->createForm(new MallaCurricularType(), $entity);
        $form->bindRequest($request);
        
         $originalModulos =array();
        foreach ($entity->getModulos() as $modulo) $originalModulos[] = $modulo;
         
            foreach ($entity->getModulos() as $modulo) {
                foreach ($originalModulos as $key => $toDel) {
                    if ($toDel->getId() === $modulo->getId()) {
                        unset($originalModulos[$key]);
                    }
                }
            }
                
             // Se quita la relacion entre modulo y malla
            foreach ($originalModulos as $modulo) {
                // remove the Task from the Tag
                $modulo->setMallaCurricular(null); //->removeElement($task);
                $em->persist($modulo);
                //$em->remove($carrera);
            }

        if ($form->isValid()) {
           
          
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('mallacurricular_edit', array('id' => $entity->getId())));
            
        }

        return array(
            'entity' => $entity,
            'edit_form'   => $form->createView()
            
            
        );
    }

    /**
     * Displays a form to edit an existing MallaCurricular entity.
     *
     * @Route("/{id}/edit", name="mallacurricular_edit")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find MallaCurricular entity.');
        }

        
        $editForm = $this->createForm(new MallaCurricularType(), $entity);
       

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
           
          
        );
    }

    /**
     * Edits an existing MallaCurricular entity.
     *
     * @Route("/{id}/update", name="mallacurricular_update")
     * @Method("post")
     * @Template("UnlHomologacionBundle:MallaCurricular:edit.html.twig")
     */
    public function updateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find MallaCurricular entity.');
        }

        
        $originalModulos =array();
        foreach ($entity->getModulos() as $modulo) $originalModulos[] = $modulo;
        
        $editForm   = $this->createForm(new MallaCurricularType(), $entity);
       

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            // filtra $originalModulos para evitar aquellas que ya no estan presentes
            foreach ($entity->getModulos() as $modulo) {
                foreach ($originalModulos as $key => $toDel) {
                    if ($toDel->getId() === $modulo->getId()) {
                        unset($originalModulos[$key]);
                    }
                }
            }
                
             // Se quita la relacion entre modulo y malla
            foreach ($originalModulos as $modulo) {
                // remove the Task from the Tag
                $modulo->setMallaCurricular(null); //->removeElement($task);
                $em->persist($modulo);
                //$em->remove($carrera);
            }
            
            
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('mallacurricular_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
           
        );
    }

    /**
     * Deletes a MallaCurricular entity.
     *
     * @Route("/{id}/delete", name="mallacurricular_delete")
     * @Method("post")
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('UnlHomologacionBundle:MallaCurricular')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find MallaCurricular entity.');
            }

            $eliminar=$entity->__toString();
            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('mallacurricular', array('eliminar'=> $eliminar)));
    }

    private function createDeleteForm($id){
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    
}
